package com.bensoft.minesweeper.gui.preferences;

import java.util.prefs.Preferences;

/**
 * Allows for the saving and reading of preferences for the minesweeper board.
 *
 * @author Ben Wolsieffer
 */
public abstract class ApplicationPreferences {

    private static int width, height, mines, beginnerHighScore, intermediateHighScore, advancedHighScore;
    private final static Preferences preferences = Preferences.userRoot().node("com/bensoft/minesweeper");
    private static boolean questionMarks, sounds;
    private static String beginnerHighScoreName, intermediateHighScoreName, advancedHighScoreName, theme;

    static {
        width = preferences.getInt("width", 9);
        height = preferences.getInt("height", 9);
        mines = preferences.getInt("mines", 10);
        questionMarks = preferences.getBoolean("marks", true);
        sounds = preferences.getBoolean("sounds", true);
        beginnerHighScore = preferences.getInt("bScore", 999);
        intermediateHighScore = preferences.getInt("iScore", 999);
        advancedHighScore = preferences.getInt("aScore", 999);
        beginnerHighScoreName = preferences.get("bName", "Anonymous");
        intermediateHighScoreName = preferences.get("iName", "Anonymous");
        advancedHighScoreName = preferences.get("aName", "Anonymous");
        theme = preferences.get("theme", "Modern");
    }

    /**
     * Sets the width of the grid that is saved in the preferences.
     *
     * @param mines The width of the grid
     * @see getWidth()
     */
    public static void setWidth(int width) {
        if (width != ApplicationPreferences.width) {
            ApplicationPreferences.width = width;
            preferences.putInt("width", width);
        }
    }

    /**
     * Sets the height of the grid that is saved in the preferences.
     *
     * @param mines The height of the grid
     * @see getHeight()
     */
    public static void setHeight(int height) {
        if (height != ApplicationPreferences.height) {
            ApplicationPreferences.height = height;
            preferences.putInt("height", height);
        }
    }

    /**
     * Gets the width of the grid as stored in the preferences.
     *
     * @return the width of the grid
     */
    public static int getWidth() {
        return width;
    }

    /**
     * Sets the number of mines that are saved in the preferences.
     *
     * @param mines The number of mines
     * @see getMines()
     */
    public static void setMines(int mines) {
        if (mines != ApplicationPreferences.mines) {
            ApplicationPreferences.mines = mines;
            preferences.putInt("mines", mines);
        }
    }

    /**
     * Gets the height of the grid as stored in the preferences.
     *
     * @return the height of the grid
     */
    public static int getHeight() {
        return height;
    }

    public static int getMines() {
        return mines;
    }

    public static boolean areQuestionMarksEnabled() {
        return questionMarks;
    }

    /**
     * Enables or disables question marks in the preferences
     * @param marks whether question marks should be enabled
     */
    public static void setQuestionMarks(boolean marks) {
        if (marks != questionMarks) {
            questionMarks = marks;
            preferences.putBoolean("marks", marks);
        }
    }

    public static boolean areSoundsOn() {
        return sounds;
    }

    public static void setSoundsOn(boolean sounds) {
        if (sounds != ApplicationPreferences.sounds) {
            ApplicationPreferences.sounds = sounds;
            preferences.putBoolean("marks", sounds);
        }
    }

    public static int getBeginnerHighScore() {
        return beginnerHighScore;
    }

    public static int getIntermediateHighScore() {
        return intermediateHighScore;
    }

    public static int getAdvancedHighScore() {
        return advancedHighScore;
    }

    public static void setBeginnerHighScore(int score) {
        if (score != beginnerHighScore) {
            beginnerHighScore = score;
            preferences.putInt("bScore", score);
        }
    }

    public static void setIntermediateHighScore(int score) {
        if (score != intermediateHighScore) {
            intermediateHighScore = score;
            preferences.putInt("iScore", score);
        }
    }

    public static void setAdvancedHighScore(int score) {
        if (score != advancedHighScore) {
            advancedHighScore = score;
            preferences.putInt("aScore", score);
        }
    }

    public static String getBeginnerHighScoreName() {
        return beginnerHighScoreName;
    }

    public static String getIntermediateHighScoreName() {
        return intermediateHighScoreName;
    }

    public static String getAdvancedHighScoreName() {
        return advancedHighScoreName;
    }

    public static void setBeginnerHighScoreName(String name) {
        if (!name.equals(beginnerHighScoreName)) {
            beginnerHighScoreName = name;
            preferences.put("bName", name);
        }
    }

    public static void setIntermediateHighScoreName(String name) {
        if (!name.equals(intermediateHighScoreName)) {
            intermediateHighScoreName = name;
            preferences.put("iName", name);
        }
    }

    public static void setAdvancedHighScoreName(String name) {
        if (!name.equals(advancedHighScoreName)) {
            advancedHighScoreName = name;
            preferences.put("aName", name);
        }
    }
    
    public static String getTheme() {
        return theme;
    }

    public static void setTheme(String theme) {
        if(!ApplicationPreferences.theme.equals(theme)) {
            ApplicationPreferences.theme = theme;
            preferences.put("theme", theme);
        }
    }
    
    private ApplicationPreferences() {
    }
}
